var station = location.href.match(/station=([\w-]+)/);
station = station && station[1] || 'gz';

Ext.define('Admin.store.Stations', {
    extend: 'Ext.data.Store',
    model: 'Admin.model.Base',
    autoLoad: true,
    buffered: true,
    proxy: {
        type: 'ajax',
        url: '/sys/stations.json'
    },

    listeners: {
        load: function () {
            var menu = [],
                i, station, item;
            for (i = 0; i < this.data.length; i++) {
                station = this.getAt(i);
                item = this.makeItem(station.get('name'), station.get('abbr'));
                menu.push(item);
            }
            Ext.getCmp('station-switcher').setMenu(menu);
        }
    },

    makeItem: function (name, abbr) {
        var me = this;
        return {
            text: name,
            checked: abbr === station,
            handler: function () {
                me.setQueryParam('station', abbr);
            }
        };
    },

    setQueryParam: function (name, value) {
        var query = Ext.Object.fromQueryString(location.search);
        query[name] = value;
        location.search = Ext.Object.toQueryString(query);
    }
});
